package com.timoon.sample.web.role.panel;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.Options;
import com.timoon.app.utils.Constantor;
import com.timoon.app.utils.GlobalRandom;
import com.timoon.component.behavior.validateengine.ValidateBehavior;
import com.timoon.component.panel.common.utils.ValidatorFeedbackPanel;
import com.timoon.component.panel.jqgrid.JqGridPanel;
import com.timoon.component.panel.jqgrid.JqGridService;
import com.timoon.component.panel.jqgrid.action.SaveButton;
import com.timoon.component.panel.jqgrid.panel.CreatorPanel;
import com.timoon.component.panel.tree.ztree.checkboxtree.CheckboxParameter;
import com.timoon.component.panel.tree.ztree.checkboxtree.ZTreeCheckboxPanel;
import com.timoon.sample.dao.model.Role;
import com.timoon.sample.web.role.RoleCreatorService;

public class RoleCreatorPanel extends CreatorPanel {
	private static final long serialVersionUID = -5631755172700219330L;
	
	//为表单生成随机id
	private String formClass="form-"+GlobalRandom.getRandom();

	private Role role;
	
	@SpringBean
	private RoleCreatorService roleCreatorService;
	
	private JqGridService service;
	
	public RoleCreatorPanel(String id, JqGridPanel mainPanel,String recordId) {
		super(id, mainPanel,recordId);
		// TODO Auto-generated constructor stub
		
		this.service=mainPanel.getService();
		
		//初始化model
		if(recordId==null){
			role=new Role();
		}else{
			role=(Role) service.loadRecordById(recordId);
		}
		
		//表单
		Form form=new Form("wk_form");
		form.add(new AttributeModifier("class",formClass));
		this.add(form);
		
		//启动验证插件
		this.add(new ValidateBehavior("."+formClass, new Options("promptPosition","'bottomRight'")));
		
		TextField<String> textInput=new TextField<>("wk_text_input",new PropertyModel(role, "text"));
		textInput.add(new AttributeModifier("class", "validate[required,minSize[2]]"));
		form.add(textInput);
		
		TextField<String> remarkInput=new TextField<>("wk_remark_input",new PropertyModel(role, "remark"));
		remarkInput.add(new AttributeModifier("class", "validate[required,minSize[2]]"));
		form.add(remarkInput);
		
		//状态
		ChoiceRenderer statusRender=new ChoiceRenderer(){
			@Override
			public Object getDisplayValue(Object object) {
				// TODO Auto-generated method stub
				return Constantor.getStatusFromStatusMapByKey(Integer.valueOf(object.toString()));
			}
		};
		DropDownChoice statusChoice=new DropDownChoice("wk_status_select", new PropertyModel<>(role, "status"),Arrays.asList(Constantor.STATUS_ENABLE,Constantor.STATUS_DISABLE),statusRender);
		form.add(statusChoice);
		
		final Model<String[]> roleIds=new Model<String[]>(new String[]{});
		CheckboxParameter checkboxParameter=new CheckboxParameter(this.roleCreatorService.getAllResource(), "id", "pId", "text",this.roleCreatorService.getResourceIdsByRole(recordId));
		ZTreeCheckboxPanel ztree=new ZTreeCheckboxPanel("wk_role_tree",roleIds,checkboxParameter);
		form.add(ztree);
		
		AjaxButton saveBtn=new SaveButton("wk_saveBtn",mainPanel){
			private static final long serialVersionUID = 1104981176828048324L;

			@Override
			protected Map<String, Object> getInputData() {
				// TODO Auto-generated method stub
				Map<String,Object> map=new HashMap<String, Object>();
				map.put("role",role);
				map.put("resourceIds", roleIds.getObject());
				return map;
			}
			@Override
			protected ValidatorFeedbackPanel getFeedback() {
				// TODO Auto-generated method stub
				
				return feedback;
			}
		};
		form.add(saveBtn);
		saveBtn.add(ztree.getOnclickJS());
	}

	public JqGridService getService() {
		return service;
	}

	public void setService(JqGridService service) {
		this.service = service;
	}

	public RoleCreatorService getRoleCreatorService() {
		return roleCreatorService;
	}

	public void setRoleCreatorService(RoleCreatorService roleCreatorService) {
		this.roleCreatorService = roleCreatorService;
	}

}
